<template>
  <div class="login" :class="currentTheme">
<!-- 各种特效空间-->
    <div class="snow" id="snow" v-show="showSnow"></div>
    <div class="wave" id="wave" v-show="showWave"></div>
    <div class="particle" id="particle" v-show="showParticle"></div>
<!-- /各种特效空间-->
    <div class="theme-selector">
      <el-select v-model="currentTheme"
                 @change="changeTheme"
                 placeholder="切换主题"
                 size="mini"                 style="width: 120px; position: absolute; top: 10px; right: 50px;">
        <el-option
          v-for="item in themes"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <!-- 原有关闭按钮保持不变 -->
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-show="loginShow">

      <!-- Add a close button -->
      <el-button type="text" @click="toggleLoginForm(false)"
                 style="position: absolute; top: 10px; right: 10px; color:black;">
        <i class="el-icon-close"></i>
      </el-button>
      <h3 class="title">旅行者，欢迎进入AI后台</h3>
      <!--      <img src="../assets/images/login-logo.jpg" style="margin-left: 20px"/>-->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;font-size: medium"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!-- Add a toggle button -->
    <el-button v-if="!loginShow" class="circular-button" @click="toggleLoginForm(true)"
               style="position: fixed; bottom: 20px; right: 20px;" circle>登录
    </el-button>
    <!--  底部  -->
    <div class="el-login-footer">
      <span><a target="_blank" href="https://beian.miit.gov.cn/">备案号：苏ICP备2023053919号</a> </span>
    </div>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from '@/utils/jsencrypt'
import {Snow,Wave,Particle} from 'jparticles'; // 引入粒子效果库 引入雪花效果库
import { sakura } from '@/assets/js/sakura'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        code: [{required: true, trigger: "change", message: "请输入验证码"}]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      loginShow: true,
      // 当前主题
      currentTheme: 'default',
      // 主题配置
      themes: [
        { value: 'default', label: '默认主题' },
        { value: 'spring', label: '春日樱花' },
        { value: 'tech-red', label: '红色科技' },
        { value: 'tech-blue', label: '蓝色科技' }
      ],
      // 特效实例存储
      effects: {
        snow: null,
        wave: null,
        tech: null
      },
      showSnow: false,
      showWave: false,
      showParticle: false,
      snowBaseConfig: {
        // 樱花参数配置
        num: 2,
        maxR: 15,
        minR: 8,
        maxSpeed: 0.2,
        minSpeed: 0.1,
        swing: true,
        swingProbability: 0.1,
        spin: true,
        shape: [
          sakura.size1,sakura.size4
        ]
      },
      waveBaseConfig: {
        num: 1,
        line: false,
        fill: true,
        fillColor: ['#27C9E5'],
        offsetTop: 0.85,
        crestHeight: 8,
        crestCount: 3,
        speed: 0.07
      },
      particleBaseConfig: {
        // 两粒子圆心点之间的直线距离
        proximity: 90,
        // 定位点半径 100 以内（包含）的所有粒子，圆心点之间小于等于 proximity 值，则连线
        range: 100,
        // 开启视差效果
        parallax: true,
      }
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    this.applyDefaultTheme();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({path: this.redirect || "/"}).catch(() => {
            });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    toggleLoginForm(show) {
      this.loginShow = show;
    },
    // 新增主题切换方法
    changeTheme(theme) {
      this.clearEffects();
      switch(theme) {
        case 'spring':
          this.applySpringTheme();
          break;
        case 'tech-red':
          this.applyTechTheme();
          break;
        case 'tech-blue':
          this.applyTechTheme();
          break;
        default:
          this.applyDefaultTheme();
      }
    },
    // 清除所有特效
    clearEffects() {
      this.showSnow = false;
      this.showWave = false;
      this.showParticle = false;
    },
    //默认主题
    applyDefaultTheme() {
      this.showWave = true;
      this.effects.wave = new Wave('#wave', {
        ...this.waveBaseConfig
      });
    },
    // 春日樱花主题
    applySpringTheme() {
      this.showSnow = true;
      this.showWave = true;
      this.effects.snow = new Snow('#snow', {
        ...this.snowBaseConfig
      });
      this.effects.wave = new Wave('#wave', {
        ...this.waveBaseConfig
      });
    },
    // 科技主题
    applyTechTheme() {
      this.showParticle = true;
      // 使用jparticles的其他特效
      this.effects.tech = new Particle('#particle', {
        ...this.particleBaseConfig
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
/* 新增主题相关样式 */
.login.spring {
  //background-image: url("../assets/images/b1.png");
}

.login.tech-red {
  background-image: url("../assets/images/b1.png");
  //.login-form {
  //  background: rgba(0, 0, 0, 0.7);
  //  color: #00ff88;
  //}
}

.login.tech-blue {
  background-image: url("../assets/images/b2.png");
}

.theme-selector {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 10;
}
#snow {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 3;
  pointer-events: none;
}
#wave {
   width: 100vw;
   height: 100vh;
   position: fixed;
   top: 0;
   left: 0;
   z-index: 4;
   pointer-events: none;
 }
#particle {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 3;
  pointer-events: none;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #f0f2f5; /* 修改背景颜色 */
  //background-image: url("../assets/images/b2.png");
  background-size: cover;
  background-position: center; // 让图片居中显示
  background-repeat: no-repeat; // 避免重复背景
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  z-index: 5;
  border-radius: 6px;
  //background: #ffffff;
  background: rgba(255,255,255,0.9); // 提高表单透明度
  backdrop-filter: blur(5px);
  width: 350px;
  padding: 25px 25px 5px 25px;
  position: relative; /* 添加定位属性 */
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); /* 增加阴影效果 */
  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.el-form-item__content .el-button--primary {
  background-color: #409EFF; /* 修改背景颜色 */
  border-color: #409EFF; /* 修改边框颜色 */
  color: #fff; /* 修改字体颜色 */
  border-radius: 24px; /* 圆角边框 */
  transition: background-color 0.3s, border-color 0.3s; /* 添加过渡效果 */
}

.el-form-item__content .el-button--primary:hover {
  background-color: #66b1ff; /* 鼠标悬停时的背景颜色 */
  border-color: #66b1ff; /* 鼠标悬停时的边框颜色 */
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  z-index: 5;
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.circular-button {
  z-index: 5;
  width: 80px; /* Adjust the size as needed */
  height: 80px;
  border-radius: 50%; /* Makes the button circular */
  display: flex; /* Center the content vertically and horizontally */
  align-items: center;
  justify-content: center;
  padding: 0; /* Remove default padding */
  font-size: 16px; /* Adjust the font size as needed */
}
</style>
